package com.heima.reggie.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.heima.reggie.bean.Employee;
import com.heima.reggie.utils.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
    private BaseContext baseContext;
    @Override
    public void insertFill(MetaObject metaObject) {
        Employee e = (Employee) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        metaObject.setValue("createUser" , e.getId());
        metaObject.setValue("updateUser" , e.getId());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());

        //从springsecurity中获取数据
        Employee e = (Employee) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        metaObject.setValue("updateUser" ,e.getId());
    }
}
